package com.terrier.ui.controls
{
    import com.terrier.ui.core.WxComponent;
    import com.terrier.ui.core.WxVerticalAlign;
    import com.terrier.ui.skins.IWxLabelSkin;
    import com.terrier.ui.skins.IWxSkinBase;
    import com.terrier.ui.skins.WxSkinManager;

    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFieldType;

    /**
     * 文本标签
     * @author Jarvis.w
     */
    public class WxLabel extends WxComponent
    {
        /**
         * @private
         */
        protected var _textDisplay:TextField;

        /**
         * @private
         */
        protected var _maxTextWidth:Number;

        /**
         * @private
         */
        protected var _minTextWidth:Number;

        /**
         * @private
         */
        protected var _maxTextHeight:Number;

		/**
		 * @private
		 */
        protected var _minTextHeight:Number;

        /**
         * @private
         */
        protected var _autoSize:Boolean;
		
		/**
		 * @private
		 */
		protected var _verticalAlign:String = WxVerticalAlign.MIDDLE;
		
        /**
         * 构造函数
         */
        public function WxLabel()
        {
            super();
        }
		
		/**
		 * 文本标签的垂直布局方式.
		 * @see WxVerticalAlign
		 */
		public function get verticalAlign():String
		{
			return _verticalAlign;
		}
		
		/**
		 * @private
		 */
		public function set verticalAlign(value:String):void
		{
			_verticalAlign = value;
			
			invalidateLayout();
		}

        /**
         * 指定按钮是否根据文本的长度来自动调整大小. 如果按钮自动调整大小,则为true; 否则,为false.
         * @default false
         * @return
         */
        public function get autoSize():Boolean
        {
            return _autoSize;
        }

        /**
         * @private
         */
        public function set autoSize(value:Boolean):void
        {
            if (_autoSize == value)
            {
                return;
            }

            _autoSize = value;
            setAutoSize(value);

            invalidateLayout();
        }

        /**
         * 文本标签的最大宽度. 文本标签的内容超出这个宽度时, 将自动换行当.
         * 仅当autoSize属性被设为true时生效.
         * 对这个属性赋值时, 组件会确保maxTextWidth大于或者等于minTextWidth.
         * @see #autoSize
         * @return
         */
        public function get maxTextWidth():Number
        {
            return _maxTextWidth;
        }

        /**
         * @private
         */
        public function set maxTextWidth(value:Number):void
        {
            _maxTextWidth = isNaN(minTextWidth) ? value : Math.max(value, minTextWidth);

            invalidateLayout();
        }

        /**
         * 文本标签的最小宽度, 当autoSize属性被设为true时生效
         * 对这个属性赋值时, 组件会确保minTextWidth小于或者等于maxTextWidth.
         * @see #autoSize
         * @return
         */
        public function get minTextWidth():Number
        {
            return _minTextWidth;
        }

        /**
         * @param value
         */
        public function set minTextWidth(value:Number):void
        {
            _minTextWidth = isNaN(maxTextWidth) ? value : Math.min(value, maxTextWidth);

            invalidateLayout();
        }

        /**
         * 文本标签的最大高度. 仅当autoSize属性被设为true时生效
         * 对这个属性赋值时, 组件会确保 maxTextHeight大于或者等于minTextHeight.
         * @see #autoSize
         * @return
         */
        public function get maxTextHeight():Number
        {
            return _maxTextHeight;
        }

        /**
         * @private
         */
        public function set maxTextHeight(value:Number):void
        {
            _maxTextHeight = isNaN(minTextHeight) ? value : Math.max(value, minTextHeight);

            invalidateLayout();
        }

        /**
         * 文本标签的最小高度. 仅当autoSize属性被设为true时生效.
         * 对这个属性赋值时, 组件会确保 minTextHeight 小于或者等于 maxTextHeight.
         * @see #autoSize
         * @return
         */
        public function get minTextHeight():Number
        {
            return _minTextHeight;
        }

        /**
         * @private
         */
        public function set minTextHeight(value:Number):void
        {
            _minTextHeight = isNaN(maxTextHeight) ? value : Math.min(value, maxTextHeight);

            invalidateLayout();
        }

        /**
         * 文本标签的文本内容
         * @return
         */
        public function get text():String
        {
            return _textDisplay.text;
        }

        /**
         * @private
         */
        public function set text(value:String):void
        {
            _textDisplay.text = value != null ? value : "";

            invalidateView();
            invalidateLayout();
        }

        /**
         * 文本标签内部的<code>TextField</code>对象
         * @return
         */
        public function get textField():TextField
        {
            return _textDisplay;
        }

        override public function set data(value:Object):void
        {
            super.data = value;
            if (data == null)
            {
                text = null;
            }
            else
            {
                text = data.hasOwnProperty("label") ? data.label : data as String;
            }
        }

        /**
         * @private
         */
        override public function setSkinID(skinID:String):void
        {
            setSkin(WxSkinManager.getLabelSkin(skinID));
        }

        /**
         * @private
         */
        override public function setSkin(value:IWxSkinBase):void
        {
            if (skin != null)
            {
                removeChildSafely(skin.bg);
            }

            super.setSkin(value);

            if (skin != null)
            {
                if (skin.bg != null)
                {
                    var index:int = getChildIndex(_textDisplay);
                    addChildAt(skin.bg, index);
                }

                if (skin.labelStyle != null)
                {
                    skin.labelStyle.applyStyle(_textDisplay);
                }
            }

            updateView();
            updateLayout();
        }

        /**
         * @private
         */
        override protected function initialize():void
        {
            super.initialize();

            _textDisplay = new TextField();
            _textDisplay.type = TextFieldType.DYNAMIC;
            _textDisplay.selectable = false;
            addChild(_textDisplay);

            _width = _textDisplay.width;
            _height = _textDisplay.height = 20;
        }

        /**
         * @private
         */
        protected function setAutoSize(value:Boolean):void
        {
            _textDisplay.autoSize = value ? TextFieldAutoSize.LEFT : TextFieldAutoSize.NONE;
        }

        /**
         * 设置文本的大小
         * @private
         */
        protected function measureText(minW:Number, maxW:Number, minH:Number, maxH:Number):void
        {
            setAutoSize(autoSize);

            if (!isNaN(maxW))
            {
                _textDisplay.wordWrap = false;
                if (_textDisplay.width > maxW)
                {
                    _textDisplay.width = maxW;
                    _textDisplay.wordWrap = true;
                }
            }

            if (!isNaN(minW) && _textDisplay.width < minW)
            {
                _textDisplay.width = minW;
                setAutoSize(false);
            }

            if (!isNaN(maxH))
            {
                if (_textDisplay.height > maxH)
                {
                    setAutoSize(false);
                    _textDisplay.height = maxH;
                }
            }

            if (!isNaN(minH))
            {
                if (_textDisplay.height < minH)
                {
                    setAutoSize(false);
                    _textDisplay.height = minH;
                }
            }
        }

        /**
         * @private
         */
        override protected function updateLayout():void
        {
            if (_skin == null || !skin.customLayout)
            {
                var leftPad:Number = skin != null ? skin.leftPad : 0;
                var rightPad:Number = skin != null ? skin.rightPad : 0;
                var topPad:Number = skin != null ? skin.topPad : 0;
                var bottomPad:Number = skin != null ? skin.bottomPad : 0;

                _textDisplay.x = leftPad;
                _textDisplay.y = topPad;

                if (autoSize)
                {
                    measureText(minTextWidth, maxTextWidth, minTextHeight, maxTextHeight);

                    _width = _textDisplay.width + leftPad + rightPad;
                    _height = _textDisplay.height + topPad + bottomPad;
                }
                else
                {
                    _textDisplay.width = _width - leftPad - rightPad;
					_textDisplay.height = _textDisplay.textHeight;
					
					if (_verticalAlign == WxVerticalAlign.MIDDLE)
					{
						_textDisplay.y = topPad + (_height - topPad - bottomPad - _textDisplay.height) / 2;
					}
					else if (_verticalAlign == WxVerticalAlign.BOTTOM)
					{
						_textDisplay.y = _height - bottomPad - _textDisplay.height;
					}
                }
            }

            super.updateLayout();

            if (skin != null && skin.labelStyle != null)
            {
                skin.labelStyle.applyLayout(_textDisplay);
            }
        }

        private function get skin():IWxLabelSkin
        {
            return _skin as IWxLabelSkin;
        }
    }
}
